package com.yht.weblog.admin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author: yht
 * @date: 2024/1/24 23:54
 * @description:
 **/
@Configuration
@EnableAsync
public class ThreadPoolConfig {

    /**
     * 创建并配置一个线程池任务执行器
     *
     * @return 线程池任务执行器
     */
    @Bean("threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置核心线程数
        executor.setCorePoolSize(10);
        // 设置最大线程数
        executor.setMaxPoolSize(20);
        // 设置队列容量
        executor.setQueueCapacity(100);
        // 设置线程池中线程的名称前缀
        executor.setThreadNamePrefix("WeblogThreadPool-");
        executor.initialize();

        return executor;
    }

}
